//
//  CustomPageControl.m
//  longbaobao
//
//  Created by Steven.Yung on 16/9/13.
//  Copyright © 2016年 Bylongma. All rights reserved.
//

#import "CustomPageControl.h"
#define dotW 4.5
#define activeDotW 6
#define magrin 20
@interface CustomPageControl () {
    UIImageView *activeImageView;
    UIImageView *inactiveImageView;
}

@end

@implementation CustomPageControl

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    if (self) {
//        [self load];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    
    self = [super initWithCoder:aDecoder];
    
    if (self) {

    }
    
    return self;
}

//- (void)load
//
//{
//    Method origin = class_getInstanceMethod([self class], @selector(_indicatorSpacing));
//    Method custom = class_getInstanceMethod([self class], @selector(custom_indicatorSpacing));
//    method_exchangeImplementations(origin, custom);
//}
//
//- (double)custom_indicatorSpacing
//{
//    return 12.0;
//}

//- (void)setActiveImage:(UIImage *)activeImage {
//    _activeImage = activeImage;
//    
//    [self setValue:activeImage forKey:@"_currentPageImage"];
//}
//
//- (void)setInactiveImage:(UIImage *)inactiveImage {
//    _inactiveImage = inactiveImage;
//    [self setValue:inactiveImage forKey:@"_pageImage"];
//}

- (void)setCurrentPage:(NSInteger)currentPage {
    [super setCurrentPage:currentPage];
    [self updateDots];
}

- (void)updateDots {
    
    for (NSInteger i = 0; i < [self.subviews count]; i++) {
        
        UIView *dot = [self.subviews objectAtIndex:i];
        
        UIImageView *imageView;
        
        if (i == self.currentPage) {
            imageView = [[UIImageView alloc] initWithImage:_activeImage];
            
        } else {
            imageView = [[UIImageView alloc] initWithImage:_inactiveImage];
        }
        
        CGFloat dotWidth = CGRectGetWidth(dot.bounds);
        CGFloat dotHeight = CGRectGetHeight(dot.bounds);
        CGFloat imageWidth = CGRectGetWidth(imageView.bounds);
        CGFloat imageHeight = CGRectGetHeight(imageView.bounds);
        //如果dot的大小小于图片的大小，将dot的大小改成图片的大小
        if (dotWidth < imageWidth || dotHeight < imageHeight) {
            dot.frame = CGRectMake(dot.frame.origin.x, dot.frame.origin.y, imageWidth, imageHeight);
        }
        //设置imageView的位置，使其在dot中居中
        imageView.frame = CGRectMake((dotWidth - imageWidth)/2, (dotHeight - imageHeight)/2, imageWidth, imageHeight);
        //清除上一次添加的imageView
        for (UIImageView *view in dot.subviews) {
            [view removeFromSuperview];
        }

        [dot addSubview:imageView];
        dot.backgroundColor = [UIColor clearColor];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    //计算圆点间距
    CGFloat marginX = dotW + magrin;
    
    //计算整个pageControll的宽度
    CGFloat newW = (self.subviews.count - 1 ) * marginX;
    
    //设置新frame
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, newW, self.frame.size.height);
    
    //设置居中
    CGPoint center = self.center;
    center.x = self.superview.center.x;
    self.center = center;
    
    //遍历subview,设置圆点frame
    for (int i=0; i<[self.subviews count]; i++) {
        UIImageView* dot = [self.subviews objectAtIndex:i];
        
        if (i == self.currentPage) {
            [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotW)];
        }else {
            [dot setFrame:CGRectMake(i * marginX, dot.frame.origin.y, dotW, dotW)];
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
